<?php
/**
 * User:姜亮
 * Date: 2018/8/31 ;Time: 8:45;
 * 角色校验
 */

namespace app\admin\controller;

use think\Controller;

use think\Config;
use app\admin\model\User;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule;
use think\auth\Auth;

class Role extends Controller
{
    //查看
    public function show()
    {
        $auth = Auth::instance();
        $permisses = array();
        $authGroups = AuthGroup::all(function ($query) {
            $query->order('id', 'asc');
        });
        foreach ($authGroups as $key => $item) {
            if (!empty($item->rules)) {
                $permiss[$key] = array();
                array_splice($permiss, 0, count($permiss));
                foreach (explode(',', $item->rules) as $rule) {
                    $permiss[] = AuthRule::get($rule)->title;
                }
                $permisses[$key] = implode(',', $permiss);
            }
        }
        $this->assign([
            'permisses' => $permisses,
            'userInfos' => $authGroups,
            'count' => count($authGroups)
        ]);
        return $this->fetch();

    }

    //添加
    public function role_add()
    {
        //标识
        $tag = input('role_add');
        if (empty($tag)) {
            //查询所有权限
            $authRule = AuthRule::all(function ($query) {
                $query->order('id', 'asc');
            });
            $this->assign('authRule', $authRule);
            return $this->fetch('role_add');
        } else {
            //添加更改
            $form = input('post.');
            $authGroup = new AuthGroup();
            $authGroup->title = $form['roleName'];
            $authGroup->status = 1;
            $authGroup->rules = $form['checkList'];
            $res = $authGroup->save();
            if ($res !== false) {
                return xmall_msg(200, 'ok');
            } else {
                return xmall_msg(201, 'fail');
            }
        }
    }

    //删除
    public function role_delete()
    {
        $ids = input('ids');
        if (empty($ids)) {
            $id = input('id');
            $res = AuthGroup::get($id)->delete();
            if ($res !== false) {
                return xmall_msg(200, 'ok');
            } else {
                return xmall_msg(201, 'fail');
            }
        } else {
            $res=AuthGroup::destroy($ids);
            if ($res>0){
                return xmall_msg(200, 'ok');
            }else{
                return xmall_msg(201, 'fail');
            }
        }
    }

    //更新
    public function role_update()
    {
        //标识
        $tag = input('role_tag');
        if (empty($tag)) {
            //查询所有权限
            $authRule = AuthRule::all(function ($query) {
                $query->order('id', 'asc');
            });
            //查询当前id
            $authGroup = AuthGroup::get(input('id'));
            $rules = $authGroup->rules;
            $this->assign([
                'rules' => $rules,
                'authGroup' => $authGroup,
                'authRule' => $authRule
            ]);
            return $this->fetch('role_update');
        } else {
            //添加更改
            $form = input('post.');
            $authGroup = AuthGroup::get($form['id']);
            $authGroup->title = $form['roleName'];
            $authGroup->status = 1;
            $authGroup->rules = $form['checkList'];
            $res = $authGroup->save();
            if ($res !== false) {
                return xmall_msg(200, 'ok');
            } else {
                return xmall_msg(201, 'fail');
            }
        }
    }
}